package cn.edu.ujn.food.controller;

import cn.edu.ujn.food.entity.*;
import cn.edu.ujn.food.service.*;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Controller
public class IngredientstockController {
    @Autowired
    private UserService userService;
    @Autowired
    private IngredientstockService ingredientstockService;
    @ModelAttribute("userMap")
    public Map<Integer, String> getUserMap() {
        List<User> users = userService.findAll();
        Map<Integer, String> userMap = new HashMap<>();
        for (User user : users) {
            userMap.put(user.getUserId(), user.getUserName());
        }
        return userMap;
    }

    @GetMapping("/ingredientstock/{id}")
    @ResponseBody
    public ResponseEntity<Ingredientstock> getIngredientStockById(@PathVariable("id") Integer ingredientStockId) {
        Ingredientstock ingredientStock = ingredientstockService.queryById(ingredientStockId);
        if (ingredientStock == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(ingredientStock, HttpStatus.OK);
    }

    @PutMapping("/ingredientstock/{id}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> updateIngredientStock(@PathVariable("id") Integer ingredientStockId, @RequestBody Ingredientstock ingredientstock) {
        try {
            ingredientstock.setIngreId(ingredientStockId);
            ingredientstockService.update(ingredientstock);
            return ResponseEntity.ok(Map.of("success", true, "message", "库存信息已成功更新"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "更新失败: " + e.getMessage()));
        }
    }

    @DeleteMapping("/ingredientstock/{id}")
    public ResponseEntity<Map<String, Object>> deleteIngredientStock(@PathVariable("id") Integer ingredientStockId) {
        try {
            boolean isDeleted = ingredientstockService.deleteById(ingredientStockId);
            if (isDeleted) {
                return ResponseEntity.ok(Map.of("success", true, "message", "库存信息删除成功！"));
            } else {
                return new ResponseEntity<>(Map.of("success", false, "message", "库存信息删除失败，库存不存在或已删除。"), HttpStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(Map.of("success", false, "message", "库存信息删除失败: " + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/ingredientstock")
    @ResponseBody
    public String createIngredientStock(Ingredientstock ingredientStock) {
        try {
            ingredientstockService.insert(ingredientStock);
            return "OK";
        } catch (Exception e) {
            return "FAIL";
        }
    }

    @GetMapping("/ingredientstock")
    public String listIngredientstock(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                      @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Ingredientstock ingredientstock) {
        ingredientstock.setStart((page - 1) * rows);
        ingredientstock.setRows(rows);
        Page<Ingredientstock> page5 = ingredientstockService.selectAll(ingredientstock);
        page5.setPage(page);
        model.addAttribute("page", page5);
        return "ingredientstock";
    }
}


